package com.iot.apicontrol.start;

import com.iot.apicontrol.dto.Dc3ApiControlDto;
import com.iot.apicontrol.service.Dc3ApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author 咕唧
 * @Date 2022/7/22 16:10
 * @Version 1.0
 */
@Component
public class Api2Cache {

    private boolean isBlock;

    private Dc3ApiService dc3ApiService;

    @Autowired
    public void setDc3ApiService(Dc3ApiService dc3ApiService) {
        this.dc3ApiService = dc3ApiService;
    }

    @EventListener
    public void onEvent(ContextRefreshedEvent event) {
        synchronized (dc3ApiService) {
            if (!isBlock) {
                isBlock = true;
                initDate();
            }
        }
    }

    private void initDate() {
        //加载mysql数据到redis缓存
        List<Dc3ApiControlDto> allApi = dc3ApiService.findAllApi2Dto();
    }

}
